#------------- User Settings -------------

# For TRCON and CTCON, please select the appropriate number based on the TRCON and CTCON settings of the relay:
# YY     = 1
# YDAC   = 2
# YDAB   = 3
# DACDAC = 4
# DABDAB = 5
# DABY   = 6
# DACY   = 7
# OTHER  = 8

visible TRCON = 6 #define transformer configuration based on the key above.
visible CTCON = 1 #define CT configuration based on the key above.

visible RZS = 0 #set to 0 if RZS = N, set to 1 if RZS = Y.  Set to 0 for 587-0.

visible TAP1 = 4.18 #define TAP setting for winding 1
visible TAP2 = 4.18 #define TAP setting for winding 2

#------------ Definitions ------------

#Selecting compensation matrix 

RZSCALC = IF (RZS=0,0,12)
mw1 = IF (TRCON=1, IF(CTCON = 4, RZSCALC , IF(CTCON = 5, RZSCALC, IF (CTCON = 1 , 1 , 9))), IF(TRCON= 2, IF(CTCON = 7, RZSCALC , IF(CTCON=1 , 11,9)), IF(TRCON= 3, IF(CTCON = 6, RZSCALC , IF(CTCON=1 , 1,9)), IF(TRCON= 4, IF(CTCON = 1, RZSCALC ,9), IF(TRCON= 5, IF(CTCON = 1, RZSCALC ,9), IF(TRCON= 6, IF(CTCON = 3, RZSCALC , IF(CTCON= 1 , RZSCALC ,9)), IF(TRCON= 7, IF(CTCON = 2, RZSCALC , IF(CTCON= 1 , RZSCALC ,9)), IF(TRCON= 8, IF(CTCON = 1, RZSCALC , 9),9)))))))))  
mw2 = IF (TRCON=1, IF(CTCON = 4, RZSCALC , IF(CTCON = 5, RZSCALC, IF (CTCON = 1 , 1 , 9))), IF(TRCON= 2, IF(CTCON = 7, RZSCALC , IF(CTCON=1 ,RZSCALC,9)), IF(TRCON= 3, IF(CTCON = 6, RZSCALC , IF(CTCON=1 , RZSCALC,9)), IF(TRCON= 4, IF(CTCON = 1, RZSCALC ,9), IF(TRCON= 5, IF(CTCON = 1, RZSCALC ,9), IF(TRCON= 6, IF(CTCON = 3, RZSCALC , IF(CTCON= 1 , 1 ,9)), IF(TRCON= 7, IF(CTCON = 2, RZSCALC , IF(CTCON= 1 , 11 ,9)), IF(TRCON= 8, IF(CTCON = 1, RZSCALC , 9),9)))))))))


#WINDING 1

scale1 = (mw1=0)*1 + ((mw1=1)+(mw1=11))*(1/sqrt(3)) + ((mw1=12))*(1/3)

I1W1_coeff1 =  ((1)*((mw1=0)+(mw1=1)+(mw1=11))) + ((2)*(mw1=12))
I1W1_coeff2 =  ((-1)*((mw1=1)+(mw1=12))) 
I1W1_coeff3 =  ((-1)*((mw1=11)+(mw1=12)))
I1W1C = scale1*(I1W1_coeff1*IAW1.phasor+I1W1_coeff2*IBW1.phasor+I1W1_coeff3*ICW1.phasor)*(1/(TAP1)) 

I2W1_coeff1 = ((-1)*((mw1=11)+(mw1=12)))
I2W1_coeff2 = ((1)*((mw1=0)+(mw1=1)+(mw1=11))) + ((2)*(mw1=12))
I2W1_coeff3 = ((-1)*((mw1=1)+(mw1=12)))
I2W1C = scale1*(I2W1_coeff1*IAW1.phasor+I2W1_coeff2*IBW1.phasor+I2W1_coeff3*ICW1.phasor)*(1/(TAP1)) 

I3W1_coeff1 = ((-1)*((mw1=1)+(mw1=12)))
I3W1_coeff2 = ((-1)*((mw1=11)+(mw1=12)))
I3W1_coeff3 = ((1)*((mw1=0)+(mw1=1)+(mw1=11))) + ((2)*(mw1=12))
I3W1C = scale1*(I3W1_coeff1*IAW1.phasor+I3W1_coeff2*IBW1.phasor+I3W1_coeff3*ICW1.phasor)*(1/(TAP1)) 

#WINDING 2

scale2 = (mw2=0)*1 + ((mw2=1)+(mw2=11))*(1/sqrt(3)) + ((mw2=12))*(1/3)

I1W2_coeff1 = ((1)*((mw2=0)+(mw2=1)+(mw2=11))) + ((2)*(mw2=12))
I1W2_coeff2 = ((-1)*((mw2=1)+(mw2=12)))
I1W2_coeff3 = ((-1)*((mw2=11)+(mw2=12)))
I1W2C = scale2*(I1W2_coeff1*IAW2.phasor+I1W2_coeff2*IBW2.phasor+I1W2_coeff3*ICW2.phasor)*(1/(TAP2)) 

I2W2_coeff1 = ((-1)*((mw2=11)+(mw2=12))) 
I2W2_coeff2 = ((1)*((mw2=0)+(mw2=1)+(mw2=11))) + ((2)*(mw2=12))
I2W2_coeff3 = ((-1)*((mw2=1)+(mw2=12)))
I2W2C = scale2*(I2W2_coeff1*IAW2.phasor+I2W2_coeff2*IBW2.phasor+I2W2_coeff3*ICW2.phasor)*(1/(TAP2)) 

I3W2_coeff1 = ((-1)*((mw2=1)+(mw2=12)))
I3W2_coeff2 = ((-1)*((mw2=11)+(mw2=12)))
I3W2_coeff3 = ((1)*((mw2=0)+(mw2=1)+(mw2=11))) + ((2)*(mw2=12))
I3W2C = scale2*(I3W2_coeff1*IAW2.phasor+I3W2_coeff2*IBW2.phasor+I3W2_coeff3*ICW2.phasor)*(1/(TAP2)) 


IRT1Calc = (mag(I1W1C)+mag(I1W2C))/2*TAP1
IRT2Calc = (mag(I2W1C)+mag(I2W2C))/2*TAP1
IRT3Calc = (mag(I3W1C)+mag(I3W2C))/2*TAP1
IOP1Calc = mag(I1W1C+I1W2C)*TAP1
IOP2Calc = mag(I2W1C+I2W2C)*TAP1
IOP3Calc = mag(I3W1C+I3W2C)*TAP1